package com.ruoyi.erp.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.erp.domain.DictProjectHourModule;
import com.ruoyi.erp.domain.ProjectHourReport;
import com.ruoyi.erp.domain.ProjectMember;
import com.ruoyi.erp.service.IProjectMemberService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.erp.domain.ProjectHour;
import com.ruoyi.erp.service.IProjectHourService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目工时Controller
 * 
 * @author sauger
 * @date 2022-04-06
 */
@Controller
@RequestMapping("/erp/projectHour")
public class ProjectHourController extends BaseController
{
    private String prefix = "erp/projectHour";

    @Autowired
    private IProjectHourService projectHourService;
    @Autowired
    private IProjectMemberService projectMemberService;

    @RequiresPermissions("erp:projectHour:view")
    @GetMapping()
    public String projectHour(ModelMap mmap)
    {
        //默认查询当周
        Date firstDay = DateUtils.getMondayDate();
        Date lastDay = DateUtils.addDays(firstDay,7);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        mmap.put("beginTime", format.format(firstDay));
        mmap.put("endTime", format.format(lastDay));
        return prefix + "/projectHour";
    }

    @RequiresPermissions("erp:projectHour:report:view")
    @GetMapping("/report")
    public String projectHourReport(ModelMap mmap)
    {
        Date monday = DateUtils.getMondayDate();
        Date lastday = DateUtils.addDays(monday, 7);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        mmap.put("firstDay", format.format(monday));
        mmap.put("lastDay", format.format(lastday));
        return prefix + "/report";
    }

    @RequiresPermissions("erp:projectHour:report:view")
    @PostMapping("/report")
    public String projectHourReportData(ProjectHourReport projectHourReport, ModelMap mmap)
    {

        ProjectHourReport report = projectHourService.getProjectHourReport(projectHourReport.getHolidayConfigId(),projectHourReport.getBeginDate(),projectHourReport.getEndDate());
        mmap.put("report", report);
        return prefix + "/report_post";
    }


    @GetMapping("/add/me")
    public String addMe(ModelMap mmap)
    {
        Integer userId = getUserId().intValue();
        ProjectMember search = new ProjectMember();
        search.setUserId(userId);
        search.getParams().put("projectFlag","normal");
        List<ProjectMember> projects = projectMemberService.selectProjectMemberList(search);
        mmap.put("userId", getUserId());
        mmap.put("projects", projects);
        return prefix + "/add_me";
    }

    @GetMapping("/my")
    public String myProjectHour(ModelMap mmap)
    {
        //默认查询当前月的工时
        Date firstDay = DateUtils.setDays(DateUtils.getNowDate(), 1);
        Date lastDay = DateUtils.addMonths(firstDay,1);
        lastDay = DateUtils.addDays(lastDay, -1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        mmap.put("beginTime", format.format(firstDay));
        mmap.put("endTime", format.format(lastDay));
        ProjectHour searchHour = new ProjectHour();
        searchHour.setUserId(getUserId().intValue());
        List<ProjectHour> projectHourList = projectHourService.selectProjectHourList(searchHour);
        mmap.put("projectHours", projectHourList);
        mmap.put("userId", getUserId());
        return prefix + "/my";
    }

    @GetMapping("/my/add/{type}")
    public String addMyHour(@PathVariable("type") String type, ModelMap mmap)
    {

        Integer userId = getUserId().intValue();
        ProjectMember search = new ProjectMember();
        if (type.equals("free"))
        {
            mmap.put("userId", userId);
            return  prefix + "/add_free";
        }else {
            search.setUserId(userId);
            search.getParams().put("projectFlag",type);
            List<ProjectMember> projects = projectMemberService.selectProjectMemberList(search);
            mmap.put("userId", getUserId());
            mmap.put("projects", projects);
            mmap.put("type", type);
            return prefix + "/add_me";
        }
    }

    /**
     * 查询项目工时列表
     */
    @RequiresPermissions("erp:projectHour:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProjectHour projectHour)
    {
        startPage();
        List<ProjectHour> list = projectHourService.selectProjectHourList(projectHour);
        return getDataTable(list);
    }

    /**
     * 导出项目工时列表
     */
    @RequiresPermissions("erp:projectHour:export")
    @Log(title = "项目工时", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProjectHour projectHour)
    {
        List<ProjectHour> list = projectHourService.selectProjectHourList(projectHour);
        ExcelUtil<ProjectHour> util = new ExcelUtil<ProjectHour>(ProjectHour.class);
        return util.exportExcel(list, "项目工时数据");
    }

    /**
     * 新增项目工时
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存项目工时
     */
    @RequiresPermissions("erp:projectHour:add")
    @Log(title = "项目工时", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProjectHour projectHour)
    {
        projectHour.setUserId(projectHour.getUserId().intValue());
        return toAjax(projectHourService.insertProjectHour(projectHour));
    }

    /**
     * 修改项目工时
     */
    @RequiresPermissions("erp:projectHour:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        ProjectHour projectHour = projectHourService.selectProjectHourById(id);
        mmap.put("projectHour", projectHour);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目工时
     */
    @RequiresPermissions("erp:projectHour:edit")
    @Log(title = "项目工时", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectHour projectHour)
    {
        return toAjax(projectHourService.updateProjectHour(projectHour));
    }

    /**
     * 删除项目工时
     */
    @RequiresPermissions("erp:projectHour:remove")
    @Log(title = "项目工时", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(projectHourService.deleteProjectHourByIds(ids));
    }
}
